/*//. Themexec - A Themed Execution Processor                               ../
  ../ Written by Bob Majdak Jr :: bob@opsat.net :: http://www.opsat.net     ../
  ../ This software is released under the GNU GPL.                          ../
  ../ Copyright (c) 2007 Bob Majdak Jr                                    .//*/ 

import themexec.define;

const char[] VERSION = "1.0"; 

int main(char[][] arg) {

	char[] themedir = expandTilde("~/.themexec");

	//. test theme directory before doing anything.
	if(!exists(themedir)) {
		try { mkdir(themedir); }
		catch { throw new Error("unable to create theme directory: " ~ themedir); }
	}

	themeInfo theme = new themeInfo;

	// options.
	bool write_cmds, verbose, pseudorun, no_wait_return;
	int wait;
	
	//. temp variables.
	int a;
	
	for(a = 0; a < arg.length; a++) {	

		if(a == 0) { continue; }

		switch(arg[a]) {
		
			//. quick switches.
			case("-c"): {
				write_cmds = true;
				break;
			}
			case("-n"): {
				no_wait_return = true;
				break;
			}
			case("-p"): {
				pseudorun = true;
				break;
			}
			case("-v"): {
				verbose = true;
				break;
			}
			case("-w"): {
				wait = toInt(arg[++a]);
				break;
			}
			
			//. commands.
			case("-h"): { }
			case("--help"): {
				writefln("Themexec v" ~ VERSION);
				writefln("Usage: themexec [OPTION] THEME");
				writefln("Execute theme file THEME from ~/.themexec directory.");
				writefln("");
				writefln("Valid options:");
				writefln("  -c       display list of commands to be run in theme.");
				writefln("  -n       do not wait for commands to return before continuing.");
				writefln("  -p       pseudoexec, do not actually perform commands.");
				writefln("  -v       display commands as they are run.");
				writefln("  -w <ms>  pause specified milliseconds between each command.");
				writefln("");
				writefln("This application is GNU GPL. Copyright (c) 2007 Bob Majdak Jr <bob@opsat.net>");
				return 0;
			}
			case("--version"): {
				writefln("Themexec v" ~ VERSION);
				writefln("This application is GNU GPL. Copyright (c) 2007 Bob Majdak Jr <bob@opsat.net>");
				return 0;
			}
			
			//. set theme.
			default: {
				theme.set(arg[a]);
				break;
			}			
		}
	}

	if(write_cmds) {
		theme.write_cmds();
	}
	
	theme.execute(verbose,pseudorun,no_wait_return,wait);

	return 0;
}